package controller;

import javax.swing.SwingWorker;

import model.FilesFinder;
import model.Movie;
import model.MoviesList;
import model.Storage;
import view.ImportedMoviesView;
import view.MainView;

/**.
 * Main Controller
 * @author jaubertbenjamin
 *
 */
public final class MainController {

     /**.
     * default constructor
     */
     private MainController() {
        //do nothing
     }

    /**.
     * movie list
     */
    private static MoviesList list;
    /**.
     * main view
     */
    private static MainView mainView;
    /**.
     * pop up view used to said that the movies are imported
     */
    private static ImportedMoviesView popUp;
    /**.
     * path used to know where is the saving file
     */
    private static String saveFileLocation;
    /**.
     *  check if the saving file is in json or not
     */
    private static boolean isJson;

     /**.
     * Main function
     * @param args general args
     */
     public static void main(final String[] args) {
        if (listExist()) {
            mainView = new MainView(list.transformIntoArray(), list.findMoviesNotCompleted(), list.getMovies().get(0));
        } else {
            mainView = new MainView(null , null , null);
        }
     }

     /**.
     * Launch a specific movie found by its title
     * @param title location of the movie
     */
    public static void launchMovie(final String title) {
        Movie movie = getMovie(title);
        if (movie != null) {
            movie.play();
        }
    }

    /**.
    * Launch a movie by its path
    * @param path location of the movie
    */
    public static void launchMovieByPath(final String path) {
        Movie movie = list.findMovieByPath(path);
        if (movie != null) {
            movie.play();
        }
    }

     /**.
     * Found a movie by its title
     * @param title title of the movie
     * @return movie
     */
    public static Movie getMovie(final String title) {
       return list.findMovie(title);
    }

     /**.
     * Organize the movie folder
     * @param path path to know where we can organize the library
     */
    public static void orderMovies(final String path) {
        if (list != null) {
            list.order(path);
            if (isJson) {
                saveJSON(MainController.saveFileLocation);
            } else {
                saveXML(MainController.saveFileLocation);
            }
        }
    }

     /***.
     * Add new movies to library
     * @param path path to know where the software can find the new movies
     */
     public static void addNewMovies(final String path) {
         if (path != null) {
             SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
                @Override
                protected Void doInBackground() throws Exception {
                    list = FilesFinder.findMovies(list, path);
                    if (MainController.saveFileLocation != null) {
                        if (isJson) {
                            saveJSON(MainController.saveFileLocation);
                        } else {
                            saveXML(MainController.saveFileLocation);
                        }
                    }
                    mainView.updateList(list.transformIntoArray());
                    mainView.setMovieInfo(list.returnFirstAvailableMovie());
                    mainView.updateListMoviesNotFound(list.findMoviesNotCompleted());
                    return null;
                }
                    protected void done() {
                    popUp.dispose();
                    if (MainController.saveFileLocation == null) {
                        MainController.saveFileLocation = mainView.setSaveChooser(".json");
                        saveJSON(MainController.saveFileLocation);
                    }
                }
            };
            worker.execute();
            popUp = new ImportedMoviesView();
            }
    }

     /**.
     * Save library in xml
     * @param path path where is located the xml file
     */
    public static void saveXML(final String path) {
        if (path != null) {
          Storage.xmlSerialization(list, path);
        }
    }

    /**.
     * Load a xml file
     * @param path to load the xml
    */
    public static void loadXML(final String path) {
        MainController.saveFileLocation = path;
        isJson = false;
        list = Storage.xmlDeserialization(path);
        mainView.updateList(list.transformIntoArray());
        mainView.setMovieInfo(list.returnFirstAvailableMovie());
        mainView.updateListMoviesNotFound(list.findMoviesNotCompleted());
    }

    /**.
     * check if the movies list exist into the application
     * @return true if exists else false
     */
    public static boolean listExist() {
        return list != null;
    }

    /**.
     * save library in json
     * @param path path where is located the json file
     */
    public static void saveJSON(final String path) {
        if (path != null) {
           Storage.jsonSerialization(list, path);
        }
    }

    /**.
     * load a json file
     * @param path path where is located the json file
    */
    public static void loadJSON(final String path) {
       MainController.saveFileLocation = path;
       isJson = true;
       list = Storage.jsonDeserialization(path);
       mainView.updateList(list.transformIntoArray());
       mainView.setMovieInfo(list.returnFirstAvailableMovie());
       mainView.updateListMoviesNotFound(list.findMoviesNotCompleted());
    }

    /**.
     * Find movies in the movies list
     * @param search term of search
     */
    public static void findMovies(final String search) {
       if (list != null) {
           mainView.updateList(list.searchMovies(search));
       }
    }

    /**.
     * retrieve the full library
     */
    public static void getFullLibrary() {
        if (list != null) {
           mainView.updateList(list.transformIntoArray());
        }
    }

}
